package com.steakliu.sun.server.api;

import com.steakliu.sun.common.util.IpUtil;
import com.steakliu.sun.constant.ConnectionsDisuseStrategyConstant;
import com.steakliu.sun.constant.InvokerConstant;
import com.steakliu.sun.constant.RateLimiterConstant;
import com.steakliu.sun.constant.RegistryConstant;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.ObjectUtils;

/**
 * 功能说明：
 * <p>
 * Original @Author: steakliu-刘牌, 2023-01-08  00:29
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
@Data
public class ServerConfig {
    /**
     * 服务地址
     */
    private String host;
    /**
     * 服务端口
     */
    private int port;
    /**
     * 扫描的包路径
     */
    private String scanPackage;
    /**
     * 注册中心类型
     */
    private String registryType;
    /**
     * 注册中心地址
     */
    private String registryAddress;
    /**
     * 反射调用类型
     */
    private String reflect;
    /**
     * 全局token
     */
    private String token;
    /**
     * 服务端向客户端发送心跳时间间隔
     */
    private int heartbeatInterval;
    /**
     * 心跳断开重连次数
     */
    private int heartbeatRetries;
    /**
     * Netty bossGroup线程数
     */
    private int bossGroupThreads;
    /**
     * Netty workGroup线程数
     */
    private int workGroupThreads;

    private String flowControlType;

    private boolean useFlowControl;

    private int concurrentCorePoolSize;

    private int concurrentMaximumPoolSize;

    private int maxConnections;

    private String connectionsDisuseStrategyType;
    /**
     * 是否使用限流
     */
    private boolean useRateLimit;
    /**
     * 限流类型
     */
    private String rateLimiterType;
    /**
     * 毫秒数
     */
    private int milliseconds;
    /**
     * 限流每毫秒的资源数
     */
    private int permits;
    /**
     * 流量达到上限触发策略
     */
    private String rateLimiterFailStrategy;
    /**
     * 是否使用请求缓冲
     */
    private boolean enableBuffer;
    /**
     * 缓冲类型
     */
    private String bufferType;
    /**
     * 结果缓存
     * @return
     */
    private boolean enableResultCache;
    /**
     * 缓存时长 -> 毫秒
     * @return
     */
    private int resultCacheTime;

    public ServerConfig() {
    }

    public ServerConfig(String host, int port, String scanPackage, String registryType, String registryAddress, String reflect, String token,
                        int heartbeatInterval, int heartbeatRetries, int bossGroupThreads, int workGroupThreads,boolean useFlowControl,
                        String flowControlType, int concurrentCorePoolSize, int concurrentMaximumPoolSize,
                        int maxConnections, String connectionsDisuseStrategyType,boolean useRateLimit, String rateLimiterType, int milliseconds,
                        int permits, String rateLimiterFailStrategy, boolean enableBuffer, String bufferType, boolean enableResultCache, int resultCacheTime) {
        this.host = ObjectUtils.isEmpty(host) ? IpUtil.getHost() : host;
        this.port = port;
        this.scanPackage = scanPackage;
        this.registryType = registryType;
        this.registryAddress = registryAddress;
        this.reflect = reflect;
        this.token = token;
        this.heartbeatInterval = heartbeatInterval <= 0 ? 30000 : heartbeatInterval;
        this.heartbeatRetries = heartbeatRetries <= 0 ? 3 : heartbeatRetries;
        this.workGroupThreads = workGroupThreads <= 0 ? 4 : workGroupThreads;
        this.bossGroupThreads = bossGroupThreads <= 0 ? Runtime.getRuntime().availableProcessors() : bossGroupThreads;
        this.flowControlType = flowControlType;
        this.useFlowControl = useFlowControl;
        this.concurrentCorePoolSize = concurrentCorePoolSize <=0 ? 4 : concurrentCorePoolSize;
        this.concurrentMaximumPoolSize = concurrentMaximumPoolSize <=0 ? Runtime.getRuntime().availableProcessors() :  concurrentMaximumPoolSize;
        this.maxConnections = maxConnections;
        this.connectionsDisuseStrategyType = connectionsDisuseStrategyType == null ? ConnectionsDisuseStrategyConstant.DEFAULT : connectionsDisuseStrategyType;
        this.rateLimiterType = rateLimiterType;
        this.milliseconds = milliseconds;
        this.permits = permits <= 0 ? RateLimiterConstant.PERMITS : permits;
        this.useRateLimit = useRateLimit;
        this.rateLimiterFailStrategy = rateLimiterFailStrategy;
        this.enableBuffer = enableBuffer;
        this.bufferType = bufferType;
        this.enableResultCache = enableResultCache;
        this.resultCacheTime = resultCacheTime;
    }
}
